package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.io.IOException;
import javax.swing.JTextField;
import javax.swing.JLabel;
import ClientLogic.ClientHandle;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Color;
import java.awt.Toolkit;

/**
 * This is the Main Screen Screen . 
 * @author Andrey
 * @author Igor
 *
 */
public class MainScreen extends JFrame {
	
	private static final long serialVersionUID = 1L;
	public Login l=null;
	private JPanel FirstPanel = null;
	private JButton Connectbt = null;
	private JTextField PortTextField = null;
	private JTextField IPtextfield = null;
	private JLabel Port = null;
	private JLabel IP = null;
	public String clientIP="localhost";
	public int clientPort=5555;
	public ClientHandle client;	
	public JLabel imageLabel = null;
	
	
	/**
	 * This method initializes Connectbt button
	 * which connects the client to server. 
	 * @throws Exception
	 */
	private JButton getConnectbt() {
		if (Connectbt == null) {
			Connectbt = new JButton();
			Connectbt.setText("Connect");                
			Connectbt.setSize(new Dimension(160, 30));
			Connectbt.setLocation(new Point(350, 298));
			Connectbt.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("Connect actionPerformed()"); 
					clientIP=IPtextfield.getText();
					clientPort=Integer.parseInt(PortTextField.getText());
					try {
						client = new ClientHandle(clientIP,clientPort);
						sendclient();  //send client details
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					setContentPane(l);  //launch Login window
							
				}
			});
		}
		return Connectbt;
	}

	
	/**
	 * This is the default constructor
	 */
	public MainScreen() {
		super();
		initialize();
		
	}

	/**
	 * This method initializes the Main screen . 
	 */
	private void initialize() 
	{
		this.setSize(900, 700);
		this.setContentPane(getFirstPanel());
		this.setTitle("Roberg Restaurant System");		
		Image icon = Toolkit.getDefaultToolkit().getImage("F:\\PROJECT\\ProjCode\\icon-roberg.png");
		this.setIconImage(icon);
	 	imageLabel = new JLabel(new ImageIcon("F:\\PROJECT\\ProjCode\\icon-roberg.png"));
	 	imageLabel.setLocation(603, 0);
		imageLabel.setSize(271,115);
		getContentPane().setBackground(Color.WHITE);
	 	getContentPane().add(imageLabel);			
	}

	/**
	 * This method sends the client details.
	 */
	public void sendclient()
	{
		l=new Login(this);
	}
	
	
	/**
	 * This method initializes FirstPanel
	 * 
	 */
	public JPanel getFirstPanel() 
	{
		if (FirstPanel == null) 
		{			
			IP = new JLabel();
			IP.setFont(new Font("Corbel", Font.BOLD | Font.ITALIC, 14));
			IP.setSize(new Dimension(83, 17));
			IP.setLocation(new Point(260, 267));
			IP.setText("IP Address:");
			Port = new JLabel();
			Port.setFont(new Font("Corbel", Font.BOLD | Font.ITALIC, 14));
			Port.setSize(new Dimension(38, 17));
			Port.setLocation(new Point(260, 232));
			Port.setText("Port:");
		 
			FirstPanel = new JPanel();
			FirstPanel.setLayout(null);
			FirstPanel.add(getConnectbt(), null);
			FirstPanel.add(getPortTextField(), null);
			FirstPanel.add(getIPtextfield(), null);
			FirstPanel.add(Port, null);
			FirstPanel.add(IP, null);
		}
		return FirstPanel; 
	}
	


	/**
	 * This method initializes PortTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPortTextField() 
	{
		if (PortTextField == null) 
		{
			PortTextField = new JTextField("5555");
			PortTextField.setSize(new Dimension(160, 21));
			PortTextField.setLocation(new Point(350, 230));
		}
		return PortTextField;
	}


	/**
	 * This method initializes IPtextfield	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIPtextfield() 
	{
		if (IPtextfield == null) 
		{
			IPtextfield = new JTextField("localhost");
			IPtextfield.setSize(new Dimension(160, 21));
			IPtextfield.setLocation(new Point(350, 266));
		}
		return IPtextfield;
	}
	
	
	/**
	 * This method initializes Get client data
	 * which shows the currently connected clients to 
	 * the server. 
	 * 	
	 */
	public void getclientData()
	{
		this.clientIP=IPtextfield.getText();
		this.clientPort=Integer.parseInt(PortTextField.getText());
	}
	
}  
